# -*- coding: utf-8 -*-
import requests
import re
import xlwt
import time
from bs4 import BeautifulSoup
headers = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
}
cookies={'__jsluid':'8d3f4c75f437ca82cdfad85c0f4f7c25'}
myfile=xlwt.Workbook()
wtable=myfile.add_sheet(u"信息",cell_overwrite_ok=True)
j = 0
a = 0
for i in range(800):
    url ="http://telecom.cnvd.org.cn/?max=20&offset="+str(a)
    r = requests.get(url)
    print r.status_code
    while r.status_code != 200:
        r = requests.get(url,headers=headers,cookies=cookies)
        print r.status_code
    html = r.text
    soup = BeautifulSoup(html)
    #print html

    for tag in soup.find('tbody',id='tr').find_all('a',href=re.compile('http://www.cnvd.org.cn/flaw/show')):
        print tag.attrs['href']
        wtable.write(j,0,tag.attrs['href'])
        j += 1
    a += 20
    print u"已完成%s"%(a)
filename=str(time.strftime('%Y%m%d%H%M%S',time.localtime()))+"url.xls"
myfile.save(filename)
print u"完成%s的url备份"%time.strftime('%Y%m%d%H%M%S',time.localtime())